/**
 * 基础接口
 */
import BaseType from './BaseType.js';
import GenericType from './GenericType.js';
import TypeKind from '../enums/TypeKind.js';

export default class BaseClass extends BaseType {

    /**
     * 声明的泛型清单
     * @type {[GenericType]}
     */
    declaredGenericTypes = [];
    /**
     * 默认的泛型清单
     * @type {[GenericType]}
     */
    defaultGenericTypes = [];

    constructor(kind, packageName, simpleName, declaredGenericTypes, defaultGenericTypes) {
        kind = TypeKind.validate(kind, 'base');
        super(kind, packageName, simpleName);
        if (Array.isArray(declaredGenericTypes)) {
            this.declaredGenericTypes = declaredGenericTypes.map(type => {
                if (typeof type === 'string') {
                    type = {
                        parameter: type,
                    }
                }
                return GenericType.of(type);
            });
        }
        if (Array.isArray(defaultGenericTypes)) {
            this.defaultGenericTypes = defaultGenericTypes.map(type => {
                return GenericType.of(type);
            });
        }
    }

    static of(object) {
        if (typeof object !== 'object') {
            return undefined;
        }
        let clazz = new BaseClass(object.kind, object.packageName, object.simpleName);
        clazz.update(object);
        return clazz;
    }

    update(object) {
        super.update(object);
        if (typeof object === 'object') {
            if (object.declaredGenericTypes) {
                this.declaredGenericTypes = object.declaredGenericTypes.map(type => {
                    return GenericType.of(type);
                });
            }
            if (object.defaultGenericTypes) {
                this.defaultGenericTypes = object.defaultGenericTypes.map(type => {
                    return GenericType.of(type);
                });
            }
        }
        return this;
    }

    clone() {
        return BaseClass.of(this);
    }

}
